package com.bristua.order.biz;

import com.bristua.order.dao.TOrderGoodsMapper;
import com.bristua.order.dao.TOrderMapper;
import com.bristua.order.model.TOrder;
import com.bristua.order.model.TOrderGoods;
import com.bristua.order.model.condition.TOrderGoodsExample;
import com.bristua.order.model.vo.OrderDetailGoodsVO;
import com.bristua.order.model.vo.QueryOrderDetailVO;
import com.bristua.user.dao.TUserAdressMapper;
import com.bristua.user.model.TUserAdress;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/4/3.
 */
@Service
public class QueryOrderDetailBiz {

    @Resource
    private TOrderMapper tOrderMapper;

    @Resource
    private TOrderGoodsMapper tOrderGoodsMapper;

    @Resource
    private TUserAdressMapper tUserAdressMapper;

    public QueryOrderDetailVO queryOrderDetail(String orderId){
        QueryOrderDetailVO queryOrderDetailVO = new QueryOrderDetailVO();
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(orderId);
        if(tOrder!=null){
            BeanUtils.copyProperties(tOrder,queryOrderDetailVO);
            if("1".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getOrderStatus())){
                queryOrderDetailVO.setStatus(1);
            }
            if("2".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getSendStatus())){
                queryOrderDetailVO.setStatus(2);
            }
            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "1".equals(tOrder.getConfirmStatus()) ){
                queryOrderDetailVO.setStatus(3);
            }
            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"0".equals(tOrder.getEvaluateStatus())){
                queryOrderDetailVO.setStatus(4);
            }
            if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"1".equals(tOrder.getEvaluateStatus())){
                queryOrderDetailVO.setStatus(5);
            }
            TUserAdress tUserAdress = tUserAdressMapper.selectByPrimaryKey(tOrder.getUserAddressId());

            if(tUserAdress!=null){
                queryOrderDetailVO.getReceiveGoods().setReceiveGoodsName(tUserAdress.getReceiveGoodsName());
                queryOrderDetailVO.getReceiveGoods().setReceivePhone(tUserAdress.getReceivePhone());
                queryOrderDetailVO.getReceiveGoods().setReceiveAddressName(tUserAdress.getReceiveAddressName());
            }

            queryOrderDetailVO.setRealName(tOrder.getUserName());
            queryOrderDetailVO.setLogisticsNumber(tOrder.getDeliverySn());
            TOrderGoodsExample tOrderGoodsExample = new TOrderGoodsExample();
            tOrderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
            List<TOrderGoods> tOrderGoodss = tOrderGoodsMapper.selectByExampleWithBLOBs(tOrderGoodsExample);
            if(tOrderGoodss!=null && tOrderGoodss.size()>0){
                for(TOrderGoods tOrderGoods : tOrderGoodss){
                    OrderDetailGoodsVO orderDetailGoodsVO = new OrderDetailGoodsVO();
                    BeanUtils.copyProperties(tOrderGoods,orderDetailGoodsVO);
                    BigDecimal buyNum = new BigDecimal(tOrderGoods.getBuyNum());
                    BigDecimal multiply = buyNum.multiply(new BigDecimal(tOrderGoods.getGoodsPrice()));
                    orderDetailGoodsVO.setAllGoodsPrice(multiply.toString());
                    orderDetailGoodsVO.setSkuId(tOrderGoods.getGoodsId());
                    queryOrderDetailVO.getGoods().add(orderDetailGoodsVO);
                }
            }
        }
        return queryOrderDetailVO;
    }
}
